package org.example.controller;

import org.example.param.CommentAddParam;
import org.example.service.CommentService;
import org.example.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RequestMapping("/comment")
@RestController
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * articleId
     * @param map
     * @return
     */
    @PostMapping("/findCommentByArticle")
    @PreAuthorize("permitAll()")
    public Result findCommentByArticle(@RequestBody Map<String,Long> map){
        Long articleId = map.get("articleId");
        return commentService.findCommentByArticle(articleId);
    }

    @PostMapping("delete")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result delete(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        return commentService.delete(id);
    }

    @PostMapping("add")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result add(@RequestBody @Validated CommentAddParam commentAddParam){
        return commentService.add(commentAddParam);
    }

}
